package com.research;

import com.google.api.translate.Language;
import com.google.api.translate.Translator;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class TranslateAc extends Activity implements OnItemSelectedListener,
		OnClickListener {

	static String[] FROM_LANG = { "Vietnamese", "English" };
	static String[] TO_LANG = { "Vietnamese", "English", "Japanese" };

	Spinner from_lang;
	Spinner to_lang;
	TextView origin_text;
	TextView translate_text;
	Button translate_button;
	Button wiki;
	Button web;
	String translate;

	ProgressDialog dialog;
	Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.translate);

		// get view
		from_lang = (Spinner) findViewById(R.id.from_lang);
		to_lang = (Spinner) findViewById(R.id.to_lang);
		origin_text = (TextView) findViewById(R.id.origin);
		translate_text = (TextView) findViewById(R.id.translate);
		translate_button = (Button) findViewById(R.id.translate_button);
		wiki = (Button) findViewById(R.id.wiki);
		web = (Button) findViewById(R.id.web);

		// set action
		from_lang.setOnItemSelectedListener(this);
		to_lang.setOnItemSelectedListener(this);
		translate_button.setOnClickListener(this);
		wiki.setOnClickListener(this);
		web.setOnClickListener(this);

		// from language spinner
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.from_lang, android.R.layout.simple_spinner_item);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		from_lang.setAdapter(adapter);

		// to language spinner
		adapter = ArrayAdapter.createFromResource(this, R.array.to_lang,
				android.R.layout.simple_spinner_item);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		to_lang.setAdapter(adapter);

		// get text from parent activity

		Bundle bundle = getIntent().getExtras();
		String character = bundle.getString("character");
		origin_text.setText(character);

		// handler
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				dialog.dismiss();

				// view result
				translate_text.setText(translate);
			}
		};
		// origin_text.setText("this is test");
		// translate
		// translate();
	}

	/*
	 * translate
	 */
	public void translate() {
		String from = from_lang.getSelectedItem().toString();
		String to = to_lang.getSelectedItem().toString();
		String origin_character = origin_text.getText().toString();

		// google translate
		google_translate(from, to, origin_character);

	}

	/**
	 * google translate use GoogleTranslateJavaClient library
	 * 
	 * @param from
	 * @param to
	 * @param originCharacter
	 * @return
	 */
	public void google_translate(String from, String to, String originCharacter) {
		// TODO Auto-generated method stub

		Translator translator = new Translator();
		translate = translator.translate(originCharacter, Language
				.valueOf(from), Language.valueOf(to));

	}

	/**
	 * wikipedia search
	 */
	static String wikiURL = "http://jp.m.wikipedia.org/wiki?search=";

	public void wikipedia(String keyword) {
		// TODO Auto-generated constructor stub

		Uri uri = Uri.parse(wikiURL + keyword);
		Log.i("URI", uri.toString());
		Intent intent = new Intent(Intent.ACTION_VIEW).setData(uri);
		startActivity(intent);
	}

	/**
	 * web search
	 * 
	 * @param keyword
	 */
	static String google = "http://www.google.co.jp/m/search?site=images&q=";

	public void webSearch(String keyword) {
		Uri uri = Uri.parse(google + keyword);
		Intent intent = new Intent(Intent.ACTION_VIEW).setData(uri);
		startActivity(intent);
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		// translate();
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view == translate_button) {
			dialog = ProgressDialog.show(this, "Translating",
					"Please, wait for few minute ...");

			new Thread() {
				public void run() {
					translate();
					handler.sendEmptyMessage(0);
				}
			}.start();

		} else if (view == wiki) {
			String keyword = translate_text.getText().toString();
			wikipedia(keyword);
		} else if (view == web) {
			String keyword = translate_text.getText().toString();
			webSearch(keyword);
		}
	}

}
